package ai.people.platform.system.controller;

import ai.people.core.security.support.RequestHolderUtil;
import ai.people.netmon.framework.domain.system.entity.NmSystemMenu;
import ai.people.netmon.framework.domain.system.request.NmSystemMenuAddParam;
import ai.people.netmon.framework.domain.system.request.NmSystemMenuUpdateParam;
import ai.people.netmon.framework.domain.system.vo.NmSystemMenuVO;
import ai.people.netmon.framework.model.response.ListResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.platform.system.service.SystemMenuService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 系统菜单接口
 *
 * @author yuanqinglong
 * @date 2022/3/4 15:48
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/menu")
public class SystemMenuController {


    /**
     * 系统菜单服务
     */
    private final SystemMenuService systemMenuService;


    /**
     * 获取用户菜单树
     *
     * @return {@link Result}<{@link List}<{@link NmSystemMenuVO}>>
     */
    @GetMapping("/tree")
    public Result<List<NmSystemMenuVO>> treeMenuByUserId() {
        List<NmSystemMenuVO> menuList = systemMenuService.listMenuTree(RequestHolderUtil.getUserId());
        return ResultUtil.success(ListResponse.builder(menuList));
    }

    /**
     * 全部菜单树
     *
     * @return {@link Result}<{@link List}<{@link NmSystemMenuVO}>>
     */
    @GetMapping("/treeAll")
    public Result<List<NmSystemMenuVO>> treeAll() {
        List<NmSystemMenuVO> menuList = systemMenuService.treeAll();
        return ResultUtil.success(ListResponse.builder(menuList));
    }


    /**
     * 添加系统菜单
     *
     * @param nmSystemMenuAddParam 系统菜单
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping
    public Result<?> addSystemMenu(@Validated @RequestBody NmSystemMenuAddParam nmSystemMenuAddParam) {
        NmSystemMenu nmSystemMenu = JSON.parseObject(JSONObject.toJSONString(nmSystemMenuAddParam), NmSystemMenu.class);
        systemMenuService.addMenu(nmSystemMenu);
        return ResultUtil.success();
    }

    /**
     * 修改系统菜单
     *
     * @param nmSystemMenuUpdateParam 系统菜单
     * @return {@link Result}<{@link ?}>
     */
    @PutMapping
    public Result<?> updateSystemMenu(@Validated @RequestBody NmSystemMenuUpdateParam nmSystemMenuUpdateParam) {
        NmSystemMenu nmSystemMenu = JSON.parseObject(JSONObject.toJSONString(nmSystemMenuUpdateParam), NmSystemMenu.class);
        systemMenuService.updateMenu(nmSystemMenu);
        return ResultUtil.success();
    }

    /**
     * 获取系统菜单
     *
     * @param id 菜单id
     * @return {@link Result}<{@link NmSystemMenu}>
     */
    @GetMapping("/{id}")
    public Result<NmSystemMenu> getSystemMenu(@PathVariable Long id) {
        return ResultUtil.success(systemMenuService.getMenuById(id));
    }


    /**
     * 用户菜单列表
     *
     * @param userId 用户id
     * @return {@link Result}<{@link List}<{@link NmSystemMenu}>>
     */
    @GetMapping("/listUserMenu")
    public Result<List<NmSystemMenu>> listUserMenus(@NotNull(message = "用户id不可为空") Long userId) {
        return ResultUtil.success(systemMenuService.listUserMenus(userId));
    }


    /**
     * 通过id删除菜单
     *
     * @param menuId menuId
     * @return {@link Result}<{@link ?}>
     */
    @DeleteMapping("{menuId}")
    public Result<?> deleteMenuById(@PathVariable Long menuId){
        systemMenuService.deleteMenu(menuId);
        return ResultUtil.success();
    }


}
